using System;
using System.IO;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.WebControls;

namespace PHSRAG.Insight21.Common.DownloadBarHelper
{
    /// <summary>
    /// Summary description for ReportHost.
    /// </summary>
    public partial class ReportHost : BasePage
    {
        #region Event Handlers

        /// <summary>
        /// When this page loads, it will attempt to write over itself with Excel data if it exists.
        /// If none exists, this page will do absolutely nothing.
        /// </summary>
        /// <remarks> 
        /// Session must be used here instead of PageCachePolicy because the DownloadBar has no
        /// knowledge of the PageCachePolicy.
        /// </remarks>
        /// <param name="sender">The reference to an object raising the event.</param>
        /// <param name="e">Information about the event being raised.</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string reportFilePath = (string) Session[DownloadBar.ReportFilePathKey];
                Response.ContentType = (string) Session[DownloadBar.ReportContentTypeKey];
                using (FileStream fileStream = new FileStream(reportFilePath, FileMode.Open, FileAccess.Read))
                {
                    byte[] bytes = new byte[fileStream.Length];
                    int totalBytesToRead = (int) fileStream.Length;
                    int totalBytesRead = 0;

                    while (totalBytesToRead > 0)
                    {
                        int bytesRead = fileStream.Read(bytes, totalBytesRead, totalBytesToRead);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        totalBytesRead += bytesRead;
                        totalBytesToRead -= bytesRead;
                    }

                    Page.Response.OutputStream.Write(bytes, 0, bytes.Length);
                }

                File.Delete(reportFilePath);
                Session.Remove(DownloadBar.ReportFilePathKey);
                Session.Remove(DownloadBar.ReportContentTypeKey);
            }
            Response.End();
        }

        protected void OnUnload(object sender, EventArgs e)
        {
            Dispose();
            GC.Collect(1);
            GC.WaitForPendingFinalizers();
            GC.Collect(1);
        }

        #endregion

        #region Web Form Designer generated code

        /// <summary>
        /// Wed Form Designer generated code - Initializes page
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
            this.Unload += new System.EventHandler(this.OnUnload);
        }

        #endregion
    }
}